﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cube
{
    public partial class ErrorLogForm : Form
    {
        delegate void SetErrorTextDelegateC(String Err, Color color);
        delegate void SetErrorTextDelegate(String Err);
        public event EventHandler<EventArgs> ClosedClicked;

        public ErrorLogForm()
        {
            InitializeComponent();
            richTextBox_ErrorLog.ForeColor = Color.Red;
        }

        public void SetErrorText(String Err, Color color)
        {
            if (InvokeRequired)
            {
                SetErrorTextDelegateC d = new SetErrorTextDelegateC(SetErrorText);
                this.Invoke(d, new object[] { Err, color });
                return;
            }
            richTextBox_ErrorLog.ForeColor = color;
            richTextBox_ErrorLog.AppendText(Err + "\r\n");
        }
        public void SetErrorText(String Err)
        {
            if (InvokeRequired)
            {
                SetErrorTextDelegate d = new SetErrorTextDelegate(SetErrorText);
                this.Invoke(d, new object[] { Err });
                return;
            }
            richTextBox_ErrorLog.ForeColor = Color.Red;
            richTextBox_ErrorLog.AppendText(Err+"\r\n");
        }

        protected virtual void OnClosedClicked(EventArgs e)
        {
            EventHandler<EventArgs> closedClicked = this.ClosedClicked;
            if (closedClicked != null)
            {
                closedClicked(this, new EventArgs());
            }
        }

        private void ErrorLogForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Muss hier abgefragt werden da das Closing Event der Hauptform an alle Kind-Fenster
            //weitergegeben wird und in dem Fall dann nicht das eigentliche Closing Event gecancelt werden soll
            //sondern nur dann wenn der User diese Form hier schließen möchte
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                OnClosedClicked(e);
            }
        }
    }
}
